import store from '@/store'
import bus from '@/layout/bus'

const { body } = document
const WIDTH = 992

export default {
  beforeMount() {
    window.addEventListener('resize', this.$_resizeHandler)
  },
  beforeDestroy() {},
  mounted() {
    const isMobile = this.$_isMobile()

    if (isMobile) {
      store.commit('setDevice', 'mobile')
    }
  },
  methods: {
    $_isMobile() {
      const rect = body.getBoundingClientRect()
      return rect.width - 1 < WIDTH
    },
    $_resizeHandler() {
      if (!document.hidden) {
        const isMobile = this.$_isMobile()
        store.commit('setDevice', isMobile ? 'mobile' : 'pc')

        if (isMobile) {
          bus.emit('collapse', false)
        }
      }
    }
  }
}
